home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / mgraph / xgraph.cc < prev    next >
Encoding:
Text File  |  1994-03-22  |  14.3 KB  |  224 lines  |  [TEXT/????]

  1. /* Copyright 1994 Ralph Gonzalez */
  2.  
  3. /*
  4. *    FILE:        xgraph.c
  5. *    AUTHOR:        R. Gonzalez, partially adapted from code by Don Snow
  6. *    CREATED:    April 16, 1993
  7. *
  8. *    xgraph.c contains several X Window routines to supplement
  9. *    a typical stdio-type application with a graphics window.
  10. *    Your program must #include "xgraph.h" and must call
  11. *    init_graphics() first.
  12. *    
  13. *    The device coordinate system (usually given in pixels) is norm-
  14. *    alized to -1 to 1 horizontally by default. The vertical limits
  15. *    depend on the screen's aspec*****************
  16. *    Transform view coordinate y to window coordinates
  17. ************************************************************************/
  18. static int    transform_y(double y)
  19. {
  20.     return (int) (gGraphY+(y-gViewY)*gGraphHeight/gViewHeight);
  21. }
  22.  
  23. /************************************************************************
  24. *    pen_color() sets the current drawing color. 
  25. ************************************************************************/
  26. void    pen_color(color x)
  27. {    
  28.     XSetForeground(gDisplay,gGC,gPixels[x]);
  29. }
  30.  
  31. /************************************************************************
  32. *    background_color() sets the background drawing color.
  33. ************************************************************************/
  34. void    background_color(color x)
  35. {    
  36.     XSetWindowBackground(gDisplay,gGraphWindow,gPixels[x]); 
  37. }
  38.  
  39. /************************************************************************
  40. *    bring graphics window to front. 
  41. ************************************************************************/
  42. void    graphics_to_front(void)
  43. {
  44.     XRaiseWindow(gDisplay,gGraphWindow); 
  45. }
  46.  
  47. /************************************************************************
  48. *    erase_graphics() makes the graphics window the background color. 
  49. ************************************************************************/
  50. void    erase_graphics(void)
  51. {
  52.     XClearWindow(gDisplay,gGraphWindow);
  53.     XFlush(gDisplay);
  54. }
  55.  
  56. /************************************************************************
  57. *    draw_line() is used to draw lines using view coordinates.
  58. ************************************************************************/
  59. void        draw_line(double x1,double y1,double x2,double y2)
  60. {
  61.     int        window_x1,
  62.             window_y1,
  63.             window_x2,
  64.             window_y2;
  65.  
  66.     window_x1 = transform_x(x1);
  67.     window_y1 = transform_y(y1);
  68.     window_x2 = transform_x(x2);
  69.     window_y2 = transform_y(y2);
  70.     
  71.     XDrawLine(gDisplay,gGraphWindow,gGC,window_x1,window_y1,
  72.         window_x2,window_y2);
  73.     XFlush(gDisplay);
  74.     
  75.     gPenX = window_x2;
  76.     gPenY = window_y2;
  77. }
  78.  
  79. /************************************************************************
  80. *    Move present pen position to new position using view
  81. *    coordinates.  Nothing is drawn.
  82. ************************************************************************/
  83. void    move_to(double x,double y)
  84. {
  85.     gPenX = transform_x(x);
  86.     gPenY = transform_y(y);
  87. }
  88.  
  89. /************************************************************************
  90. *    Draw from present pen position to new position using view
  91. *    coordinates.
  92. ************************************************************************/
  93. void    draw_to(double x,double y)
  94. {
  95.     int        window_x,
  96.             window_y;
  97.             
  98.     window_x = transform_x(x);
  99.     window_y = transform_y(y);
  100.     
  101.     XDrawLine(gDisplay,gGraphWindow,gGC,gPenX,gPenY,window_y,window_y);
  102.     XFlush(gDisplay);
  103.     gPenX =  window_x;
  104.     gPenY =  window_y;
  105. }
  106.  
  107. /************************************************************************
  108. *    draw_circle() draws a circle using view coordinates.
  109. ************************************************************************/
  110. void draw_circle(double center_x,double center_y,double r)
  111. {
  112.     int                window_x,
  113.                     window_y,
  114.                     window_r,
  115.                     start_angle = 0,
  116.                     path_angle = 360*64;    /* in 1/64 of degrees */
  117.     unsigned int    width,
  118.                     height;
  119.             
  120.     window_x = transform_x(center_x);
  121.     window_y = transform_y(center_y);
  122.     window_r = (int) (r*gGraphWidth/gViewWidth);
  123.     window_x = window_x - window_r;
  124.     window_y = window_y - window_r;
  125.     width = window_r*2;
  126.     height = width;    
  127.     
  128.     XDrawArc(gDisplay,gGraphWindow,gGC,window_x,window_y,width,height,
  129.         start_angle,path_angle);
  130.     XFlush(gDisplay);
  131.     gPenX =  window_x;
  132.     gPenY =  window_y;
  133. }
  134.  
  135. /************************************************************************
  136. *    fill_circle() draws a circle using view coordinates.  The circle
  137. *    is filled with the present pen color
  138. ************************************************************************/
  139. void fill_circle(double center_x,double center_y,double r)
  140. {
  141.     int                window_x,
  142.                     window_y,
  143.                     window_r,
  144.                     start_angle = 0,
  145.                     path_angle = 360*64;    /* in 1/64 of degrees */
  146.     unsigned int    width,
  147.                     height;
  148.             
  149.     window_x = transform_x(center_x);
  150.     window_y = transform_y(center_y);
  151.     window_r = (int) (r*gGraphWidth/gViewWidth);
  152.     window_x = window_x - window_r;
  153.     window_y = window_y - window_r;
  154.     width = window_r*2;
  155.     height = width;    
  156.     
  157.     XFillArc(gDisplay,gGraphWindow,gGC,window_x,window_y,width,height,
  158.         start_angle,path_angle);
  159.     XFlush(gDisplay);
  160.     gPenX =  window_x;
  161.     gPenY =  window_y;
  162. }
  163.  
  164. /************************************************************************
  165. *    mouse_button_is_down() checks whether the mouse button is down,
  166. *    returns TRUE or FALSE.
  167. ************************************************************************/
  168. boolean        mouse_button_is_down(void)
  169. {
  170.     XEvent    theEventDummy;
  171.     
  172.     if (XPending(gDisplay))
  173.     {
  174.         XNextEvent(gDisplay,&theEventDummy);
  175.         return TRUE;
  176.     }
  177.     else
  178.         return FALSE;
  179. }
  180.  
  181. /************************************************************************
  182. *    wait until button is pressed
  183. ************************************************************************/
  184. void    wait(void)
  185. {
  186.     XEvent    theEventDummy;
  187.   
  188.     XNextEvent(gDisplay,&theEventDummy);
  189. }
  190.  
  191. /************************************************************************
  192. *    get_mouse_location returns the mouse coordinates in terms of the 
  193. *    view coordinate system. It uses the inverse transformation of that
  194. *    in transform_x() and transform_y().
  195. ************************************************************************/
  196. void    get_mouse_location(double *x_ptr,double *y_ptr)
  197. {
  198.     static Window    dummy_root,dummy_child; /* static so we don't have
  199.                                             to allocate them repeatedly */
  200.     boolean            in_screen;
  201.     int                root_x,
  202.                     root_y,
  203.                     x,
  204.                     y;
  205.     unsigned int    button_state;
  206.     
  207.     in_screen = XQueryPointer(gDisplay,gGraphWindow,&dummy_root,
  208.         &dummy_child,&root_x,&root_y,&x,&y,&button_state);
  209.     
  210.     if (in_screen)
  211.     {
  212.         *x_ptr = gViewX+(x-gGraphX)*gViewWidth/gGraphWidth;
  213.         *y_ptr = gViewY+(y-gGraphY)*gViewHeight/gGraphHeight;
  214.     }
  215.     else
  216.     {
  217.         *x_ptr = 0.;
  218.         *y_ptr = 0.;
  219.     }
  220.  
  221.     return;
  222. }
  223.  
  224.